package so.wy.servicecenter.businessbed.manager.inner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ManagerException;
import so.wy.servicecenter.base.frame.manager.AbstractManager;
import so.wy.servicecenter.businessbed.dao.enums.StatusDevicePublishEnum;
import so.wy.servicecenter.businessbed.dao.enums.StatusDeviceRunEnum;
import so.wy.servicecenter.businessbed.dao.rdb.device.DeviceDO;
import so.wy.servicecenter.businessbed.dao.rdb.order.order.OrderDO;
import so.wy.servicecenter.businessbed.manager.fm.DeviceFManager;
import so.wy.servicecenter.businessbed.manager.fm.OrderFManager;

@Service
@Scope("prototype")
public class DeviceIManager extends AbstractManager {
    @Autowired
    private DeviceFManager deviceFManager;

    @Autowired
    private OrderFManager orderFManager;

    //床进行租赁
    public void deviceCreateOrder(Long deviceId){
        deviceFManager.updateDeviceRunStatus(deviceId,StatusDeviceRunEnum.RETURNABLE);
    }

    //床已归还
    public void deviceFinishOrder(Long deviceId){
        deviceFManager.updateDeviceRunStatus(deviceId,StatusDeviceRunEnum.RENTABLE);
    }

    //检验床是否可以发起订单
    public DeviceDO checkDeviceCanOrder(String deviceSerial) throws ManagerException {
        DeviceDO deviceDO = checkDeviceExist(deviceSerial);

        //判断床是否可租赁状态
        if (StatusDevicePublishEnum.STOP.getCode().equals(deviceDO.getDevicePublishStatus())){
            throw exception("该设备上架状态异常，请联系客服人员!");
        }
        if (!StatusDeviceRunEnum.RENTABLE.getCode().equals(deviceDO.getDeviceStatus())){
            throw exception("该设备不可租赁！");
        }

//        OrderDO runOrder = orderFManager.getDeviceRunOrder(deviceSerial);
//        if (runOrder!=null){
//            throw exception("设备正在租赁中!");
//        }

        return deviceDO;
    }

    //校验床是否可以归还
    public DeviceDO checkDeviceReturnable(String deviceSerial) throws ManagerException {
        //判断床是否存在
        DeviceDO deviceDO = checkDeviceExist(deviceSerial);

        //判断锁是否在已租赁状态
        if (StatusDevicePublishEnum.STOP.getCode().equals(deviceDO.getDevicePublishStatus())){
            throw exception("该设备上架状态异常，请联系客服人员!");
        }
        if (!StatusDeviceRunEnum.RETURNABLE.getCode().equals(deviceDO.getDeviceStatus())){
            throw exception("该设备不可归还！");
        }

        return deviceDO;
    }

    public DeviceDO checkDeviceExist(String deviceSerial) throws ManagerException {
        DeviceDO deviceInfo = deviceFManager.getDeviceInfo(deviceSerial);

        if (deviceInfo == null){
            throw exception("设备信息不存在!");
        }

        return deviceInfo;
    }

    public DeviceDO getDeviceInfo(String deviceSerial){
        return deviceFManager.getDeviceInfo(deviceSerial);
    }

}
